package cn.anlab.anappframe.widget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.anlab.anappframe.R;

/**
 * 对话框组件
 * 
 * @version 1.0
 * @author Stan Rong 2012-3-3
 */
public class CompDialog {

	private Context context;
	private AlertDialog dialog;
	private View contentView;
	
	public static final int BUTTON1 = -1;
	public static final int BUTTON2 = -2;
	public static final int BUTTON3 = -3;
	
	//按钮及按键事件
	private Button[] buttons = {null, null, null};
	private DialogInterface.OnClickListener[] buttonClickListeners = {null, null, null};

	public CompDialog(Context context) {
		this.context = context;
		dialog = new AlertDialog.Builder(context).create();
		dialog.show();
		Window win = dialog.getWindow();
		win.setContentView(R.layout.comp_dialog);
		dialog.dismiss();
	}
	
	public CompDialog(Context context, String title) {
		this(context);
		setTitle(title);
	}
	
	public CompDialog(Context context, String title, String message) {
		this(context);
		setTitle(title);
		setMessage(message);
	}

	public CompDialog(Context context, String title, View contentView) {
		this(context);
		setTitle(title);
		setContentView(contentView);
	}
	
	/**
	 * 设置标题
	 * @param title
	 */
	public void setTitle(String title) {
		TextView tvTitle = (TextView)dialog.findViewById(R.id.comp_dialog_title);
		tvTitle.setText(title);
	}
	
	/**
	 * 设置要显示的文本内容
	 * @param message
	 */
	public void setMessage(String message) {
		TextView tvMessage = (TextView)dialog.findViewById(R.id.comp_dialog_message);
		tvMessage.setVisibility(View.VISIBLE);
		tvMessage.setText(message);
	}
	
	
	/**
	 * 设置内容区域
	 * @param contentView
	 */
	public void setContentView(View contentView) {
		this.contentView = contentView;
		LinearLayout center = (LinearLayout)dialog.findViewById(R.id.comp_dialog_center);
		center.removeAllViews();
		center.addView(contentView);
	}
	
	/**
	 * 设置按钮及事件
	 * @param buttonCode
	 * @param dialogClickListener
	 */
	public void setButton(final int buttonCode, final String text,  final DialogInterface.OnClickListener dialogClickListener) {
		Button button = null;
		switch (buttonCode) {
		case BUTTON1:
			button = (Button)dialog.findViewById(R.id.comp_dialog_button1);
			break;
		case BUTTON2:
			button = (Button)dialog.findViewById(R.id.comp_dialog_button2);
			break;
		case BUTTON3:
			button = (Button)dialog.findViewById(R.id.comp_dialog_button3);
			break;
		default:
			throw new IllegalArgumentException("方法setButton(int,DialogInterface.OnClickListener)的第1个参数，只支持BUTTON1, BUTTON2, BUTTON3.");
		}
		button.setText(text);
		button.setVisibility(View.VISIBLE);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialogClickListener.onClick(dialog, buttonCode);
			}
		});
	}
	
	
	
	/**
	 * 显示对话框
	 */
	public void show() {
		dialog.show();
	}
	
	/**
	 * 取消对话框
	 */
	public void dismiss() {
		dialog.dismiss();
	}

	
}
